﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using System.Security.Cryptography;

namespace PanelLogowania
{
    public partial class PanelLogowania : Form
    {
        public PanelLogowania()
        {
            InitializeComponent();
            Zalogowano = false;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
        }

        bool Zalogowano;

        private void label3_Click(object sender, EventArgs e)
        {

        }

        public string CalculateMD5Hash(string input)
        {
            // step 1, calculate MD5 hash from input
            MD5 md5 = System.Security.Cryptography.MD5.Create();
            byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(input);
            byte[] hash = md5.ComputeHash(inputBytes);

            // step 2, convert byte array to hex string
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < hash.Length; i++)
            {
                sb.Append(hash[i].ToString("X2"));
            }
            return sb.ToString();
        }


        private void button2_Click_1(object sender, EventArgs e)
        {
            this.Close();
        }

        private void PanelLogowania_FormClosed(object sender, FormClosedEventArgs e)
        {

        }

        private void label3_Click_1(object sender, EventArgs e)
        {
            Rejestracja Panel = new Rejestracja(this);
            Panel.ShowDialog();
        }

        private void button1_Click(object sender, EventArgs e)
        {

            if (textBox1.Text != "" && textBox2.Text != "")
            {
                string Password = "";
                string MyConString = "SERVER=178.162.175.80;" +
                "PORT=3306;" +
                "DATABASE=quiz;" +
                "UID= bg;" +
                "PASSWORD=2Yftm4EvRME4YWFM;";
                MySqlConnection connection = new MySqlConnection(MyConString);
                MySqlCommand command = connection.CreateCommand();
                MySqlDataReader Reader;
                command.CommandText = "select password from users where nick=" + "'" + textBox1.Text.ToString() + "'";
                try
                {
                    connection.Open();
                    Reader = command.ExecuteReader();
                    while (Reader.Read())
                    {
                        string thisrow = "";
                        thisrow += Reader.GetValue(0).ToString();
                        Password = thisrow;
                    }
                    connection.Close();
                    if (Password != "" && Password == CalculateMD5Hash(textBox2.Text))
                    {
                        Zalogowano = true;
                        MessageBox.Show("Poprawnie zalogowano!");
                        this.Close();

                    }
                    else
                    {
                        MessageBox.Show("Podałeś nieprawidłową nazwę użytkownika lub hasło!");
                    }
                }
                catch (MySqlException ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
            else
            {
                MessageBox.Show("Pola Login i Hasło są wymagane!");
            }
        }
    }
}
